//
//  PTLocationManager.swift
//  PointTest28
//
//  Created by sc-chen on 2023/7/8.
//

import CoreLocation

class PTLocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
    private let locationManager = CLLocationManager()

    @Published var currentLocation: CLLocationCoordinate2D?

    override init() {
        super.init()
        self.locationManager.delegate = self
    }

    func requestLocation(accuracy: CLLocationAccuracy) {
        locationManager.desiredAccuracy = accuracy
        locationManager.requestWhenInUseAuthorization()

        if CLLocationManager.locationServicesEnabled() {
            locationManager.requestLocation()
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let location = locations.last {
            currentLocation = location.coordinate
            print("经度: \(location.coordinate.longitude), 纬度: \(location.coordinate.latitude)")
        }
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("Location error: \(error.localizedDescription)")
    }
}
